package com.example.xinyun;

import cn.droidlover.xrecyclerview.XRecyclerView;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.TbTz;
import com.example.xinyun.provider.TzOwnProvider;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;
import java.util.List;

public class TzOwnAbility extends Ability {
    private ArrayList<Object> data;
    private TzOwnProvider provide;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_tz_own_layout);
        findComponentById(ResourceTable.Id_tz).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder().withDeviceId("")
                        .withBundleName("com.xijing.xinyun")
                        .withAbilityName("com.example.xinyun.FbAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent,0);
            }
        });
        data = new ArrayList<>();
        initData();
        XRecyclerView listContainer = (XRecyclerView) findComponentById(ResourceTable.Id_xr);
        listContainer.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                data.clear();
                initData();
            }

            @Override
            public void onLoadMore() {

            }


        });
        provide = new TzOwnProvider(this, data);
        listContainer.setItemProvider(provide);
    }
    private void initData() {
        String url = UrlContant.URL+"selecttz?user="+MyApplication.username;
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {

            @Override
            //http访问出错，此部分在主线程中工作,可以更新UI等操做。
            public void onFailure(int code, String errorMessage) {
                new ToastDialog(TzOwnAbility.this)
                        .setText("加载失败，请检查网络连接！")
                        .show();
            }

            @Override
            //http访问成功，此部分在主线程中工作，可以更新UI等操作。
            public void onResponse(String response) {
                //将http请求返回的json字符串转换成一个dbPsychological类的对象
                List<TbTz> list = new Gson().fromJson(response.trim(), new TypeToken<List<TbTz>>() {
                }.getType());
                if (list != null) {
                    if (data.size() != 0) {
                        data.clear();
                    }
                    data.addAll(list);
                    provide.notifyDataChanged();
                }
            }
        });
    }
}
